package com.ruoyi.system.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.TbMember;
import com.ruoyi.system.mapper.TbMemberMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TbMemberRecordsMapper;
import com.ruoyi.system.domain.TbMemberRecords;
import com.ruoyi.system.service.ITbMemberRecordsService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;

/**
 * 会员权益使用记录表Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-05-18
 */
@Service
public class TbMemberRecordsServiceImpl implements ITbMemberRecordsService 
{
    @Autowired
    private TbMemberRecordsMapper tbMemberRecordsMapper;

    @Resource
    private TbMemberServiceImpl memberService;

    /**
     * 查询会员权益使用记录表
     * 
     * @param id 会员权益使用记录表主键
     * @return 会员权益使用记录表
     */
    @Override
    public TbMemberRecords selectTbMemberRecordsById(Long id)
    {
        return tbMemberRecordsMapper.selectTbMemberRecordsById(id);
    }

    /**
     * 查询会员权益使用记录表列表
     * 
     * @param tbMemberRecords 会员权益使用记录表
     * @return 会员权益使用记录表
     */
    @Override
    public List<TbMemberRecords> selectTbMemberRecordsList(TbMemberRecords tbMemberRecords)
    {
        return tbMemberRecordsMapper.selectTbMemberRecordsList(tbMemberRecords);
    }

    /**
     * 新增会员权益使用记录表
     * 
     * @param tbMemberRecords 会员权益使用记录表
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertTbMemberRecords(TbMemberRecords tbMemberRecords)
    {
        tbMemberRecords.setCreateTime(DateUtils.getNowDate());
        TbMember tbMember = memberService.selectTbMemberById(tbMemberRecords.getMemberId());
        tbMemberRecords.setMemberName(tbMember.getMemberName());
        tbMemberRecordsMapper.insertTbMemberRecords(tbMemberRecords);
        switch (Integer.parseInt(tbMemberRecords.getMemberType()+"")){
            case 1:
                checkNumber(tbMember.getEquity1(),1,tbMember.getMemberName());
                tbMember.setEquity1(tbMember.getEquity1()-1L);
            case 2:
                checkNumber(tbMember.getEquity2(),2,tbMember.getMemberName());
                tbMember.setEquity2(tbMember.getEquity2()-1L);
            case 3:
                checkNumber(tbMember.getEquity3(),3,tbMember.getMemberName());
                tbMember.setEquity3(tbMember.getEquity3()-1L);
            case 4:
                checkNumber(tbMember.getEquity4(),4,tbMember.getMemberName());
                tbMember.setEquity4(tbMember.getEquity4()-1L);
            case 5:
                checkNumber(tbMember.getEquity5(),5,tbMember.getMemberName());
                tbMember.setEquity5(tbMember.getEquity5()-1L);
            case 6:
                checkNumber(tbMember.getEquity6(),6,tbMember.getMemberName());
                tbMember.setEquity6(tbMember.getEquity6()-1L);
            case 7:
                checkNumber(tbMember.getEquity7(),7,tbMember.getMemberName());
                tbMember.setEquity7(tbMember.getEquity7()-1L);
            case 8:
                checkNumber(tbMember.getEquity8(),8,tbMember.getMemberName());
                tbMember.setEquity8(tbMember.getEquity8()-1L);
            case 9:
                checkNumber(tbMember.getEquity9(),9,tbMember.getMemberName());
                tbMember.setEquity9(tbMember.getEquity9()-1L);
            case 10:
                checkNumber(tbMember.getEquity10(),10,tbMember.getMemberName());
                tbMember.setEquity10(tbMember.getEquity10()-1L);
            default:
        }
        memberService.updateTbMember(tbMember);
        return 1;
    }

    /**
     * 修改会员权益使用记录表
     * 
     * @param tbMemberRecords 会员权益使用记录表
     * @return 结果
     */
    @Override
    public int updateTbMemberRecords(TbMemberRecords tbMemberRecords)
    {
        return tbMemberRecordsMapper.updateTbMemberRecords(tbMemberRecords);
    }

    /**
     * 批量删除会员权益使用记录表
     * 
     * @param ids 需要删除的会员权益使用记录表主键
     * @return 结果
     */
    @Override
    public int deleteTbMemberRecordsByIds(Long[] ids)
    {
        return tbMemberRecordsMapper.deleteTbMemberRecordsByIds(ids);
    }

    /**
     * 删除会员权益使用记录表信息
     * 
     * @param id 会员权益使用记录表主键
     * @return 结果
     */
    @Override
    public int deleteTbMemberRecordsById(Long id)
    {
        return tbMemberRecordsMapper.deleteTbMemberRecordsById(id);
    }

    private boolean checkNumber(Long equityNumber,Integer memberType,String memberName){
        if (ObjectUtils.isEmpty(equityNumber) ||equityNumber-1L< 0){
            throw new RuntimeException(memberName+"的权益"+memberType+"不足1次！");
        }
        return true;

    }
}
